<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item v-if="isSchoolLevel" label="校区/学院/年级/专业班级" prop="areaCollegeGradeProClass"
                    label-width="170px">
        <el-cascader
          v-model="queryParams.areaCollegeGradeProClass"
          :options="stuClassCasList"
          :props="casProps"
          placeholder="试试搜索：数信"
          filterable
          @change="handleChange"></el-cascader>
      </el-form-item>
      <el-form-item v-if="queryParams.schoolAreaIds.length>0&&!isSchoolLevel" label="学院/年级/专业班级"
                    prop="collegeGradeProClass"
                    label-width="150px">
        <el-cascader
          v-model="queryParams.collegeGradeProClass"
          :options="collegeGradeProClassList"
          :props="casProps"
          placeholder="试试搜索：数信"
          filterable
          @change="handleChange"></el-cascader>
      </el-form-item>
      <el-form-item v-if="queryParams.collegeIds.length>0" label="年级/专业班级" prop="gradeProClass" label-width="120px">
        <el-cascader
          v-model="queryParams.gradeProClass"
          :options="gradeProClassList"
          placeholder="试试搜索：19软工2班"
          :props="casProps"
          filterable
          @change="handleChange"></el-cascader>
      </el-form-item>
      <!--宿舍层面-->
      <el-form-item v-if="isSchoolLevel ||queryParams.schoolAreaIds.length>0||queryParams.collegeIds.length>0"
                    label="楼栋/楼层/房间号" prop="areaBuildingFloorRoom"
                    label-width="150px">
        <el-cascader
          v-model="queryParams.areaBuildingFloorRoom"
          :options="areaBuildingFloorRoomList"
          placeholder="试试搜索：A3"
          :props="casProps"
          filterable
          @change="handleChange"></el-cascader>
      </el-form-item>
      <el-form-item v-if="queryParams.buildingIds.length>0" label="楼层/房间号" prop="floorRoomBed" label-width="150px">
        <el-cascader
          v-model="queryParams.floorRoom"
          :options="floorRoomBedNoList"
          :props="casProps"
          placeholder="试试搜索：410"
          filterable
          @change="handleChange"></el-cascader>
      </el-form-item>
      <!--楼层长-->
      <el-form-item v-if="queryParams.floorIds.length>0" label="房间号/床位号" prop="roomBedId" label-width="120px">
        <el-cascader
          v-model="queryParams.roomBedId"
          :options="rooms"
          :props="casProps"
          placeholder="试试搜索：410"
          filterable
          @change="handleChange"
        ></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery()">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery()">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportStatistics"
        >导出
        </el-button>
      </el-form-item>
    </el-form>

    <div >
      <el-table
        ref="table"
        v-loading="loading"
        :data="tableData"
        show-summary
        height="500"
      >
        <el-table-column align="center"
                         width="75"
                         label="学籍或寝室信息"
                         prop="name">
        </el-table-column>
        <el-table-column align="center"
                         width="60"
                         label="本科生"
                         prop="underNum"
        >
        </el-table-column>
        <el-table-column align="center"
                         width="65"
                         label="研究生"
                         prop="postNum">
        </el-table-column>
        <el-table-column align="center"
                         label="学生总数"
                         prop="shouldNum"
        >
        </el-table-column>
        <el-table-column align="center"
                         label="当前在校"

        >
          <el-table-column align="center" label="本科生" prop="atSchoolUnderNum" width="60"></el-table-column>
          <el-table-column align="center" label="研究生" prop="atSchoolPostNum" width="60"></el-table-column>
        </el-table-column>
        <el-table-column align="center"
                         label="因事因病请假"

        >
          <el-table-column align="center" label="本科生" prop="underHolidayNum" width="60">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('本科请假学生列表',scope.row,10,1)">{{scope.row.underHolidayNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="研究生" prop="postHolidayNum" width="60">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('研究生请假学生列表',scope.row,10,0)">{{scope.row.postHolidayNum}}</span>
              </el-link>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column align="center"
                         label="本科生不在校（寝）情况"

        >
          <el-table-column align="center" label="入伍" prop="underSoldierNum" width="50">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('本科应征入伍学生列表',scope.row,3,1)">{{scope.row.underSoldierNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="休学" prop="underRestNum" width="50">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('本科休学学生列表',scope.row,2,1)">{{scope.row.underRestNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="延毕" prop="underDelayGraduationNum" width="50">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('本科延毕生学生列表',scope.row,6,1)">{{scope.row.underDelayGraduationNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="交换生" prop="underExchangeNum" width="60">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('本科交换生学生列表',scope.row,4,1)">{{scope.row.underExchangeNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="校外实习" prop="underPracticeNum" width="75">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('本科校外实习学生列表',scope.row,5,1)">{{scope.row.underPracticeNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="联合培养" prop="underJointSupervisionNum" width="75">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('本科联合培养（校外）学生列表',scope.row,7,1)">{{scope.row.underJointSupervisionNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="走读" prop="underExternNum" width="50">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('本科走读学生列表',scope.row,8,1)">{{scope.row.underExternNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="其他" prop="otherNum" width="50">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('本科其他学生列表',scope.row,9,1)">{{scope.row.otherNum}}</span>
              </el-link>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column align="center"
                         label="研究生不在校（寝）情况"

        >
          <el-table-column align="center" label="入伍" prop="postSoldierNum" width="50">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('研究生应征入伍学生列表',scope.row,3,0)">{{scope.row.postSoldierNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="休学" prop="postRestNum" width="50">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('研究生休学学生列表',scope.row,2,0)">{{scope.row.postRestNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="延毕" prop="postDelayGraduationNum" width="50">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('研究生延毕生学生列表',scope.row,6,0)">{{scope.row.postDelayGraduationNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="交换生" prop="postExchangeNum" width="60">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('研究生交换生学生列表',scope.row,4,0)">{{scope.row.postExchangeNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="校外实习" prop="postPracticeNum" width="75">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('研究生校外实习学生列表',scope.row,5,0)">{{scope.row.postPracticeNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="联合培养" prop="postJointSupervisionNum" width="75">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('研究生联合培养（校外）学生列表',scope.row,7,0)">{{scope.row.postJointSupervisionNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="走读" prop="postExternNum" width="50">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('研究生走读学生列表',scope.row,8,0)">{{scope.row.postExternNum}}</span>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="其他" prop="postOtherNum" width="50">
            <template slot-scope="scope">
              <el-link type="primary"><span @click="showNotAtSchoolReasonDialog('研究生其他学生列表',scope.row,9,0)">{{scope.row.postOtherNum}}</span>
              </el-link>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>

    </div>
    <!--休学学生表格对话框 -->
    <el-dialog :close-on-click-modal="false" :title="notAtSchoolReasonTitle" :visible.sync="dialogTableVisible"
               :fullscreen="true" @closed="closeNotAtSchoolDialog">
      <el-form label-position="left" :model="queryNotAtSchoolReasonParams" ref="queryNotAtSchoolReasonParams"
               :inline="true">
        <el-form-item v-if="isSchoolLevel" label="" prop="areaCollegeGradeProClass"
                      label-width="170px">
          <el-cascader
            v-model="queryNotAtSchoolReasonParams.areaCollegeGradeProClass"
            :options="stuClassCasList"
            :props="casProps"
            placeholder="校区/学院/年级/专业班级"
            filterable
            @change="queryNotSchoolByStuClass"></el-cascader>
        </el-form-item>
        <el-form-item v-if="this.queryParams.schoolAreaIds.length>0" label="学院/年级/专业班级"
                      prop="collegeGradeProClass"
                      label-width="150px">
          <el-cascader
            v-model="queryNotAtSchoolReasonParams.collegeGradeProClass"
            :options="collegeGradeProClassList"
            :props="casProps"
            placeholder="试试搜索：数信"
            filterable
            @change="queryNotSchoolByStuClass"></el-cascader>
        </el-form-item>
        <el-form-item v-if="this.queryParams.collegeIds.length>0" label="年级/专业班级" prop="gradeProClass"
                      label-width="120px">
          <el-cascader
            v-model="queryNotAtSchoolReasonParams.gradeProClass"
            :options="gradeProClassList"
            placeholder="试试搜索：19软工2班"
            :props="casProps"
            filterable
            @change="queryNotSchoolByStuClass"></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh" type="primary" size="mini" @click="resetQueryNotAtSchoolReasonParams()">重置
          </el-button>
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >导出
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :data="notAtSchoolReasonStus" v-loading="notAtSchoolReasonLoading">
        <el-table-column type="selection" width="50" align="center"/>
        <el-table-column label="姓名" align="center" prop="name" width="70"/>
        <el-table-column label="学院" align="center" prop="collegeName" :show-overflow-tooltip="true"/>
        <el-table-column label="年级" align="center" prop="gradeName" width="60"/>
        <el-table-column label="专业班级" align="center" prop="proClassName"/>
        <el-table-column label="性别" align="center" width="50">
          <template slot-scope="scope">
            <span v-if="scope.row.sex===0">男</span>
            <span v-if="scope.row.sex===1">女</span>
          </template>
        </el-table-column>
        <el-table-column label="类别" align="center" width="65">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.stu_type" :value="scope.row.type"/>
          </template>
        </el-table-column>
        <el-table-column label="学号" align="center" prop="stuNo" width="130"/>
        <el-table-column label="身份证号" align="center" prop="idNum"/>
        <el-table-column label="校区" align="center" width="65">
          <template slot-scope="scope">
            <span v-if="scope.row.schoolArea===1">新校区</span>
            <span v-if="scope.row.schoolArea===2">老校区</span>
          </template>
        </el-table-column>
        <el-table-column label="住宿信息" align="center" width="90">
          <template slot-scope="scope">
            <span>{{scope.row.buildingName}}-{{scope.row.roomName}}-{{scope.row.bedName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="手机号" align="center" prop="phone" width="108"/>
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.stu_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="现居地址" align="center" prop="addrNow" :show-overflow-tooltip="true"/>
      </el-table>
      <pagination
        v-show="notAtSchoolReasonTotal>0"
        :total="notAtSchoolReasonTotal"
        :page.sync="queryNotAtSchoolReasonParams.pageNum"
        :limit.sync="queryNotAtSchoolReasonParams.pageSize"
        @pagination="getNotAtSchoolReasonList"
      />
    </el-dialog>

  </div>
</template>

<script>
  import {findChildrenStuClassCasById, findChildrenStuClassCasByIds} from "@/api/stu/class";
  import {
    notAtSchoolList,
    oldAndNewStatistics,
    childrenStatistics,
    notAtSchoolReasonList,
  } from "@/api/stu/stu";
  import {
    findChildrenDorCasById,
    findAreaBuildingFloorRoom,
    findFloorRoom,
    findFloorRoomByParentIds,
    findChildrenByParentId
  } from "@/api/stu/dor";
  import {getInfo} from "@/api/login";
  import {getUserRoleByUserId} from "@/api/stu/userRole";

  export default {
    name: "index",
    dicts: ['stu_status', 'stu_type'],
    data() {
      return {
        elTableHeight:null,
        queryParams: {
          schoolArea: null,
          schoolAreaIds: [],
          collegeId: null,
          collegeIds: [],
          gradeId: null,
          proClassId: null,
          proClassIds: [],
          buildingId: null,
          buildingIds: [],
          floorId: null,
          floorIds: [],
          roomId: null,
          bedId: null,
          collegeGradeProClass: [],
          gradeProClass: [],
          floorRoomBedId: [],
          roomBedId: [],
          userRoles: []
        },
        queryNotAtSchoolReasonParams: {
          pageNum: 1,
          pageSize: 10,
          schoolArea: null,
          collegeId: null,
          gradeId: null,
          proClassId: null,
          buildingId: null,
          floorId: null,
          roomId: null,
          bedId: null,
          collegeGradeProClass: [],
          gradeProClass: [],
          floorRoomBedId: [],
          roomBedId: [],
        },
        //是否为全校管理员
        isSchoolLevel: false,
        showSearch: true,
        //新老校区的所有班级
        stuClassCasList: [],
        stuDorCasList: [],
        collegeGradeProClassList: [],
        areaBuildingFloorRoomList: [],
        gradeProClassList: [],
        floorRoomBedNoList: [],
        roomBedNoList: [],
        rooms: [],
        casProps: {
          label: 'label',
          value: 'id',
          checkStrictly: true
        },
        userRoles: [],
        userId: null,
        loading: false,
        tableData: [],
        dialogTableVisible: false,
        notAtSchoolReasonStus: [],
        notAtSchoolReasonTotal: 0,
        notAtSchoolReasonLoading: false,
        notAtSchoolReasonTitle: '',
        total: 0,
        schoolArea: null,
        collegeId: null,
        proClassId: null,
        buildingId: null,
        floorId: null
      }
    },
    updated() {
      this.$nextTick(() => {
        this.$refs['table'].doLayout();
      })
    },
    created() {
      this.getUserInfo()
      this.getStuClassCas()
      this.getStuDorCas()
      this.getAreaBuildingFloorRoom()
    },
    methods: {
      getNotAtSchoolList() {
        notAtSchoolList(this.queryParams).then(res => {

        })
      },
      closeNotAtSchoolDialog() {
        this.queryNotAtSchoolReasonParams = {
          pageSize: 10,
          isMultiRoles: false
        }
        this.dialogTableVisible = false
      },
      resetQueryNotAtSchoolReasonParams() {
        this.queryNotAtSchoolReasonParams.areaCollegeGradeProClass = []
        this.queryNotAtSchoolReasonParams.collegeGradeProClass = []
        this.queryNotAtSchoolReasonParams.gradeProClass = []
        this.getNotAtSchoolReasonList()
      },
      getStatistics() {
        this.loading = true
        childrenStatistics(this.queryParams).then(res => {
          this.tableData = res.rows
          this.loading = false
          this.total = res.total
        })
      },
      showNotAtSchoolReasonDialog(title, row, status, type) {
        this.notAtSchoolReasonTitle = title
        this.handleNotAtSchoolReason(row);
        this.queryNotAtSchoolReasonParams.status = status
        this.queryNotAtSchoolReasonParams.type = type
        this.getNotAtSchoolReasonList()
        this.dialogTableVisible = true
      },
      getNotAtSchoolReasonList() {
        this.notAtSchoolReasonLoading = true
        notAtSchoolReasonList(this.queryNotAtSchoolReasonParams).then(res => {
          this.notAtSchoolReasonStus = res.rows
          this.notAtSchoolReasonTotal = res.total
          this.notAtSchoolReasonLoading = false
        })
      },
      handleNotAtSchoolReason(row) {
        if (row.schoolArea) this.queryNotAtSchoolReasonParams.schoolArea = row.schoolArea
        if (row.collegeId) this.queryNotAtSchoolReasonParams.collegeId = row.collegeId
        if (row.gradeId) this.queryNotAtSchoolReasonParams.gradeId = row.gradeId
        if (row.proClassId) this.queryNotAtSchoolReasonParams.proClassId = row.proClassId
        if (row.buildingId) this.queryNotAtSchoolReasonParams.buildingId = row.buildingId
        if (row.floorId) this.queryNotAtSchoolReasonParams.floorId = row.floorId
        if (row.roomId) this.queryNotAtSchoolReasonParams.roomId = row.roomId
      },
      handleChange(data) {
        this.getStatistics()
        this.isCasChanged = true
      },
      queryNotSchoolByStuClass(data) {
        this.getNotAtSchoolReasonList()
      },
      handleQuery() {

      },
      resetQuery() {
        this.$tab.refreshPage()
      },
      getStuClassCas() {
        findChildrenStuClassCasById({parentId: 0}).then(res => {
          this.stuClassCasList = res.data
        })
      },
      getStuDorCas() {
        findChildrenDorCasById({parentId: 0}).then(res => {
          this.stuDorCasList = res.data
        })
      },
      getUserInfo() {
        getInfo().then(res => {
          this.userId = res.user.userId
          this.getUserRoles()
        })
      },
      getAreaBuildingFloorRoom() {
        findAreaBuildingFloorRoom().then(res => {
          this.areaBuildingFloorRoomList = res.data
        })

      },
      getUserRoles() {
        getUserRoleByUserId(this.userId).then(res => {
          this.userRoles = res.data
          if (this.userRoles.length > 1) {
            this.queryParams.isMultiRoles = true
          }
          let isOldAdmin = false
          let isNewAdmin = false
          this.userRoles.forEach(item => {
            this.queryParams.userRoles.push(item.roleType + '-' + item.roleId)
            if (item.roleType === 1) this.queryParams.schoolAreaIds.push(item.roleId)
            if (item.roleType === 2) this.queryParams.collegeIds.push(item.roleId)
            if (item.roleType === 3) this.queryParams.proClassIds.push(item.roleId)
            if (item.roleType === 4) this.queryParams.buildingIds.push(item.roleId)
            if (item.roleType === 5) this.queryParams.floorIds.push(item.roleId)
            if (item.roleId === 1) isNewAdmin = true
            if (item.roleId === 2) isOldAdmin = true
          })
          if (isOldAdmin && isNewAdmin) {
            this.isSchoolLevel = true
            this.getOldAndNewStatistics()
          }
          this.initProps()
        })
      },
      getOldAndNewStatistics() {
        this.loading = true
        oldAndNewStatistics().then(res => {
          this.tableData = res.rows
          this.total = res.total
          this.loading = false
        })
      },
      initProps() {
        //  校级领导通过学院进行学生的条件查询
        if (this.queryParams.schoolAreaIds.length > 0 && !this.isSchoolLevel) {
          this.getCollegeGradeProClassList(this.queryParams.schoolAreaIds)
          this.getStatistics()
        }
        if (this.queryParams.collegeIds.length > 0) {
          this.getGradeProClassList(this.queryParams.collegeIds)
          this.getStatistics()
        }
        if (this.queryParams.proClassIds.length > 0) {
          this.getStatistics()
        }
        if (this.queryParams.buildingIds.length > 0) {
          this.getFloorRoomList(this.queryParams.buildingIds)
          this.getStatistics()
        }
        if (this.queryParams.floorIds.length > 0) {
          this.getRoomBedList(this.floorId)
          this.getStatistics()
        }
      },
      getCollegeGradeProClassList(schoolAreaIds) {
        findChildrenStuClassCasByIds(schoolAreaIds).then(res => {
          this.collegeGradeProClassList = res.data
        })
      },
      getGradeProClassList(collegeIds) {
        findChildrenStuClassCasByIds(collegeIds).then(res => {
          this.gradeProClassList = res.data
        })
      },
      getFloorRoomList(buildingIds) {
        findFloorRoomByParentIds(buildingIds).then(res => {
          this.floorRoomBedNoList = res.data
        })
      },
      getRoomBedList(floorId) {
        findChildrenByParentId({parentId: floorId}).then(res => {
          this.rooms = res.data
        })
      },
      handleExport() {
        this.download('stuManage/stu/notAtSchoolReasonListExport', {
          ...this.queryNotAtSchoolReasonParams
        }, `stu_${new Date().getTime()}.xlsx`)
      },
      handleExportStatistics() {
        this.download('stuManage/stu/childrenStatisticsExport', this.queryParams, `stu_${new Date().getTime()}.xlsx`)
      }
    }
  }
</script>

<style scoped>
</style>
